from rest_framework import serializers
from .models import (
    PageView, SearchQuery, UserActivity,
    SalesReport, PopularBook, TrafficSource,
    ConversionFunnel, ABNTest
)


class PageViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = PageView
        fields = ['id', 'path', 'user', 'session_id', 'created_at']
        read_only_fields = ['id', 'created_at']


class SearchQuerySerializer(serializers.ModelSerializer):
    class Meta:
        model = SearchQuery
        fields = ['id', 'query', 'user', 'results_count', 'has_results', 'created_at']
        read_only_fields = ['id', 'created_at']


class UserActivitySerializer(serializers.ModelSerializer):
    class Meta:
        model = UserActivity
        fields = ['id', 'user', 'activity_type', 'path', 'data', 'created_at']
        read_only_fields = ['id', 'created_at']


class SalesReportSerializer(serializers.ModelSerializer):
    class Meta:
        model = SalesReport
        fields = [
            'id', 'report_type', 'start_date', 'end_date',
            'total_revenue', 'book_sales', 'shipping_fee',
            'total_orders', 'completed_orders', 'cancelled_orders', 'refund_orders',
            'report_file', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class PopularBookSerializer(serializers.ModelSerializer):
    book_title = serializers.CharField(source='book.title', read_only=True)
    book_author = serializers.CharField(source='book.author', read_only=True)
    
    class Meta:
        model = PopularBook
        fields = [
            'id', 'book', 'book_title', 'book_author',
            'view_count', 'sales_count', 'popularity_score', 'date'
        ]
        read_only_fields = ['id', 'date']


class TrafficSourceSerializer(serializers.ModelSerializer):
    class Meta:
        model = TrafficSource
        fields = [
            'id', 'source', 'medium', 'campaign', 'content',
            'visits', 'conversions', 'conversion_rate', 'date'
        ]
        read_only_fields = ['id', 'date']


class ConversionFunnelSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConversionFunnel
        fields = [
            'id', 'step_name', 'visitors', 'conversions',
            'conversion_rate', 'drop_off_rate', 'date'
        ]
        read_only_fields = ['id', 'date']


class ABNTestSerializer(serializers.ModelSerializer):
    class Meta:
        model = ABNTest
        fields = [
            'id', 'name', 'test_type', 'description', 'hypothesis',
            'start_date', 'end_date', 'status', 'winner',
            'visitors_a', 'conversions_a', 'visitors_b', 'conversions_b',
            'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class DashboardStatsSerializer(serializers.Serializer):
    total_users = serializers.IntegerField()
    total_orders = serializers.IntegerField()
    total_revenue = serializers.DecimalField(max_digits=10, decimal_places=2)
    total_books = serializers.IntegerField()
    today_orders = serializers.IntegerField()
    today_revenue = serializers.DecimalField(max_digits=10, decimal_places=2)
    pending_orders = serializers.IntegerField()
    low_stock_books = serializers.IntegerField()


class SalesTrendSerializer(serializers.Serializer):
    date = serializers.DateField()
    orders = serializers.IntegerField()
    revenue = serializers.DecimalField(max_digits=10, decimal_places=2)


class UserActivityReportSerializer(serializers.Serializer):
    date = serializers.DateField()
    new_users = serializers.IntegerField()
    active_users = serializers.IntegerField()
    page_views = serializers.IntegerField()
    unique_visitors = serializers.IntegerField()


class SearchAnalyticsSerializer(serializers.Serializer):
    query = serializers.CharField()
    count = serializers.IntegerField()
    conversion_rate = serializers.FloatField()


class TopSellingBookSerializer(serializers.Serializer):
    book_id = serializers.IntegerField()
    book_title = serializers.CharField()
    total_sold = serializers.IntegerField()
    total_revenue = serializers.DecimalField(max_digits=10, decimal_places=2)


class CategoryPerformanceSerializer(serializers.Serializer):
    category_name = serializers.CharField()
    total_sales = serializers.IntegerField()
    total_revenue = serializers.DecimalField(max_digits=10, decimal_places=2)
    average_rating = serializers.FloatField()
